<template>
  <div class="home-container">
    <!-- 顶部导航栏组件 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="search"
        >搜索</van-button
      >
    </van-nav-bar>
    <div>
      <van-tabs class="channel-tabs" v-model="active" animated swipeable>
        <van-tab
          v-for="channel in channels"
          :title="channel.name"
          :key="channel.id"
        >
          <article-list ref="article-list" :channel="channel" />
        </van-tab>
        <div slot="nav-right" class="placeholder"></div>
        <div slot="nav-right" class="hamburger-btn" @click="isChannelEditShow = true">
          <i class="heima hm-gengduo"></i>
        </div>
      </van-tabs>
      <van-popup
        v-model="isChannelEditShow"
        closeable
        round
        close-icon-position="top-left"
        position="bottom"
        :style="{ height: '80%' }"
      >
        <channel-edit :my-channels="channels" :active-index="active" @update-active="onUpdateActive"/>
      </van-popup>
    </div>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage.js'
import ArticleList from '../../components/article-list.vue'
import ChannelEdit from '../../components/channel-edit.vue'
export default {
  name: 'HomeIndex',
  computed: {
    ...mapState(['user'])
  },
  data () {
    return {
      channels: [],
      active: 0,
      isChannelEditShow: false // 控制编辑频道弹出层的显示状态
    }
  },
  components: {
    ArticleList,
    ChannelEdit
  },
  created () {
    this.loadChannels()
  },
  methods: {
    async loadChannels () {
      // try {
      //   const { data } = await getUserChannels()
      //   this.channels = data.data.channels
      //   console.log(this.channels)
      // } catch (err) {
      //   this.$toast('获取频道数据失败')
      // }

      // const用来声明常量，而这里的channels根据登录类型的不同是个变量
      let channels = []
      if (this.user) {
        const { data } = await getUserChannels()
        channels = data.data.channels
      } else {
        const localChannels = getItem('TOUTIAO_CHANNELS')
        if (localChannels) {
          channels = localChannels
        } else {
          const { data } = await getUserChannels()
          channels = data.data.channels
        }
      }
      this.channels = channels
    },
    onUpdateActive (index, isChannelEditShow = true) {
      this.active = index
      this.isChannelEditShow = isChannelEditShow
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  /deep/ .van-tabs__nav--line {
    padding-right: 0 !important;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }

    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777777;
    }

    .van-tab--active {
      color: red;
    }

    .van-tabs__nav {
      padding-bottom: 0;
    }

    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }

    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 70px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902;

      i.heima {
        font-size: 33px;
      }

      &:before {
        content: "";
        position: absolute;
        left: 0;
        width: 2px;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
  padding-top: 174px;
  padding-bottom: 100px;
  .article-list {
    height: 79vh;
    overflow-y: auto;
  }
}
</style>
